package com.photo.tagger;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;

public class TestActivity extends Activity implements OnTouchListener {
	Bitmap bit;
	TagEdit tagView;
	Tag tagData;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main);
		myView imageCanvas;
		imageCanvas = new myView(this);
		LinearLayout parent = new LinearLayout(this);
		parent.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
		LayoutParams.FILL_PARENT));
		//parent.addView(imageCanvas,new LayoutParams(LayoutParams.WRAP_CONTENT,
			//	LayoutParams.WRAP_CONTENT));

		setContentView(imageCanvas);
		imageCanvas.setOnTouchListener(this);
	}

	String TAG = "tag";

	public class myView extends View {

		int fH, fW;

		public myView(Context context) {
			super(context);

			// TODO Auto-generated constructor stub
		}

		@Override
		protected void onDraw(Canvas canvas) {
			// TODO Auto-generated method stub
			Bitmap myBitmap = decodeSampledBitmapFromResource(getResources(),
					R.drawable.image1, 640, 480);
			
			// canvas.scale(320, 240);
			int screenH = getWindowManager().getDefaultDisplay().getHeight();
			int screenW = getWindowManager().getDefaultDisplay().getWidth();
			int imgW = myBitmap.getWidth();
			int imgH = myBitmap.getHeight();
			
			
			
			Log.i(TAG, "screen width" + screenW + "screen height" + screenH
					+ "image width" + imgW + "image height" + imgH);
			calculateFittableSize(screenW, screenH, imgW, imgH);

			// calculating mid point
			int midX=screenW/2;
			int midY=screenH/2;
			//calculating first pixel point to place bitmap in the middle
			int firstX=fW/2, firstY=fH/2;
			
			float centreX = (canvas.getWidth()  - fW) /2;

			float centreY = (canvas.getHeight() - fH) /2;
			
			canvas.drawBitmap(
					Bitmap.createScaledBitmap(myBitmap, fW, fH, true), centreX, centreY,
					null);
			
		}
	

		protected void calculateFittableSize(int scrW, int scrH, int imgW,
				int imgH) {

			float imgR = (float) imgH / imgW;
			Log.i(TAG, "imag Ratio" + imgR);
			if (imgH > imgW) {
				fH = scrH;
				float ratio = (float) imgH / scrH;
				fW = (int) (imgW / ratio);
			} else {
				fW = scrW;
				float ratio = (float) imgW / scrW;
				fH = (int) (imgH / ratio);
			}

			float fitRatio = (float) fH / fW;
			Log.i(TAG, "" + fitRatio);
		}
		protected void drawTagUI(Canvas canvas)
		{
			tagView=new TagEdit(getApplicationContext(),tagData.getXcoordinate(),tagData.getYcoordinate());
			
		}
	}

	public boolean onTouch(View view, MotionEvent event) {
		// TODO Auto-generated method stub
		// if (view == imageCanvas) {
		int x = (int) event.getX();
		int y = (int) event.getY();
		Toast.makeText(TestActivity.this, "touched at " + x + "," + y,
				Toast.LENGTH_SHORT).show();
		tagData=new Tag(1,"jfdks",x,y,true);
		
		
		// }
		return false;
	}

	public int calculateInSampleSize(BitmapFactory.Options options,
			int reqWidth, int reqHeight) {
		// Raw height and width of image
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;

		if (height > reqHeight || width > reqWidth) {
			if (width > height) {
				inSampleSize = Math.round((float) height / (float) reqHeight);
			} else {
				inSampleSize = Math.round((float) width / (float) reqWidth);
			}
		}
		return inSampleSize;
	}

	public Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
			int reqWidth, int reqHeight) {

		// First decode with inJustDecodeBounds=true to check dimensions
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeResource(res, resId, options);

		// Calculate inSampleSize
		options.inSampleSize = calculateInSampleSize(options, reqWidth,
				reqHeight);

		// Decode bitmap with inSampleSize set
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeResource(res, resId, options);
	}

}